/**
 * 
 */
package OfficeServer.office.request;

import java.io.Serializable;
import java.util.ArrayList;

import OfficeServer.office.Office;
import OfficeServer.office.request.RequestException.REASON;
import OfficeServer.office.request.UserRequest.TYPE;
import OfficeServer.report.Appointment;

/**
 * @author Chris Bayruns
 * 
 */
public class AppointmentRequest extends OfficeRequest implements Serializable {
    /**
	 * 
	 */
	//private static final long serialVersionUID = 4936426954006657672L;

	/**
     * @author Chris Bayruns
     * 
     */
    public enum TYPE {
	REQUEST_APPOINTMENT, CONFIRM_APPOINTMENT, UPDATE_APPOINTMENT, DELETE_APPOINTMENT, CREATE_APPOINTMENT, GET_WAITING_APPOINTMENTS, USER_APPOINTMENTS, USER_NUM_APPOINTMENTS, CHECK_CALENDER_FREE, GET_APPOINTMENTS_BY_DAY
    }

    /**
     * @author Chris Bayruns
     */
    protected TYPE type;
    protected boolean self = false;

    public AppointmentRequest(TYPE type, Object... myArgs) {
	this.type = type;
	this.setArgs(myArgs);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * OfficeServer.office.request.OfficeRequest#processRequest(OfficeServer
     * .office.Office)
     */
    @Override
    public void processRequest(Office in) {
	switch (type) {
	case REQUEST_APPOINTMENT:
	    processRequestAppt(in);
	    break;
	case GET_WAITING_APPOINTMENTS:
	    processWaitingAppt(in);
	    break;
	case CONFIRM_APPOINTMENT:
	    processConfirmAppt(in);
	    break;
	case USER_APPOINTMENTS:
	    processGetUserAppts(in);
	    break;
	case UPDATE_APPOINTMENT:
	    processUpdateAppt(in);
	    break;
	case DELETE_APPOINTMENT:
	    processDeleteAppt(in);
	    break;
	case CHECK_CALENDER_FREE:
	    processCheckCal(in);
	    break;
	case GET_APPOINTMENTS_BY_DAY:
	    processApptByDay(in);
	    break;
	case USER_NUM_APPOINTMENTS:
	    processNumAppointments(in);
	    break;
	}
    }

    private void processNumAppointments(Office in) {
	try {
	    data = new Object[1];
	    if (self) {
		data[0] = in.getSelfAppointments();
	    } else {
		data[0] = in.getSearchUserNumAppointments();
	    }
	} catch (Exception e) {
	    error = e;
	}
    }

    /**
     * @author Chris Bayruns
     * @param in
     *            The office object to act on
     */
    private void processApptByDay(Office in) {
	try {
	    if (args.length == 1) {
		data = new Object[1];
		data[0] = (ArrayList<Appointment>) in
			.getAppointmentsByDay((Integer) args[0]);
	    } else {
		throw (new RequestException(
			RequestException.REASON.INVALID_NUMBER_OF_ARGUMENTS));
	    }
	} catch (Exception e) {
	    error = e;
	}
    }

    /**
     * @author Chris Bayruns
     * @param in
     *            The office object to act on
     */
    private void processCheckCal(Office in) {
	try {
	    if (args.length >= 1) {
		data = new Object[1];
		data[0] = in.selfCheckCalendar((Integer) args[0]);
	    } else {
		throw (new RequestException(
			RequestException.REASON.INVALID_NUMBER_OF_ARGUMENTS));
	    }
	} catch (Exception e) {
	    error = e;
	}
    }

    /**
     * @author Chris Bayruns
     * @param in
     *            The office object to act on
     */
    private void processDeleteAppt(Office in) {
	try {
	    if (args.length == 1) {
		in.deleteSearchUserAppointment((Appointment) args[0]);
	    } else {
		throw (new RequestException(
			RequestException.REASON.INVALID_NUMBER_OF_ARGUMENTS));
	    }
	} catch (Exception e) {
	    error = e;
	}
    }

    /**
     * @author Chris Bayruns
     * @param in
     *            The office object to act on
     */
    private void processUpdateAppt(Office in) {
	try {
	    if (args.length == 1) {
		in.updateSearchUserAppointment((Appointment) args[0]);
	    } else {
		throw (new RequestException(
			RequestException.REASON.INVALID_NUMBER_OF_ARGUMENTS));
	    }
	} catch (Exception e) {
	    error = e;
	}
    }

    /**
     * @author Chris Bayruns
     * @param in
     *            The office object to act on
     */
    private void processGetUserAppts(Office in) {
	try {
	    data = new Object[1];
	    if (self) {
		data[0] = in.getSelfAppointments();
	    } else {
		if (args.length == 1) {
		    data[0] = in.getSearchUserAppointments((Integer)args[0]);
		}
		else
		{
		    data[0] = in.getSearchUserAppointments();
		}
	    }
	} catch (Exception e) {
	    error = e;
	}
    }

    /**
     * @author Chris Bayruns
     * @param in
     *            The office object to act on
     */
    private void processConfirmAppt(Office in) {
	try {
	    if (args.length == 1) {
		in.confirmSearchUserAppointment((Appointment) args[0]);
	    } else {
		throw (new RequestException(
			RequestException.REASON.INVALID_NUMBER_OF_ARGUMENTS));
	    }
	} catch (Exception e) {
	    error = e;
	}
    }

    /**
     * @author Chris Bayruns
     * @param in
     *            The office object to act on
     */
    private void processWaitingAppt(Office in) {
	ArrayList<Appointment> temp = null;
	try {
	    data = new Object[1];
	    temp = in.getWaitingAppointments();
	    data[0] = temp;
	} catch (Exception e) {
	    error = e;
	}
    }

    /**
     * @author Chris Bayruns
     * @param in
     *            The office object to act on
     */
    private void processRequestAppt(Office in) {
	try {
	    // (int docID, int day, int timeSlot)
	    if (args.length == 3) {
		in.addSelfAppointment((Integer) args[0], (Integer) args[1],
			(Integer) args[2]);
	    } else {
		error = new RequestException(REASON.INVALID_NUMBER_OF_ARGUMENTS);
	    }
	} catch (Exception e) {
	    error = e;
	}
    }

}
